//
//  DDCustomTextView.swift
//  swift微博
//
//  Created by whitcast on 16/7/20.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit

class DDCustomTextView: UITextView {
    //添加图片保存的视图
    lazy var pictureView: DDPictureCollectionView = {
        
        let view = DDPictureCollectionView()
        //设置尺寸
        let margin = 10
        
        let width1 = UIScreen.mainScreen().bounds.size.width - CGFloat(2 * margin)
        
        let height = width1
        
        view.size = CGSize(width: width1, height: height)
        
        view.backgroundColor = UIColor.lightGrayColor()
        
        return view
    
    }()
    
    //构造方法
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        //设置UI
        setupUI()
        //监听文本变化
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textChanged", name: UITextViewTextDidChangeNotification, object: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //监听文本的改变
   @objc private func textChanged(){
        
         placeHolderLab.hidden = hasText()
    }
    
    // MARK:--重写text属性
    override var text: String?{
        didSet{
            //判断是否隐藏placeholderlab
            placeHolderLab.hidden = hasText()
            
            }
    }
    //placeholder文字
    private lazy var placeHolderLab: UILabel = {
        let lab = UILabel()
        lab.text = "写下你今天的心情吧,有没有遇到什么有趣的事情?写下你今天的心情吧,有没有遇到什么有趣的事情?写下你今天的心情吧,有没有遇到什么有趣的事情?写下你今天的心情吧,有没有遇到什么有趣的事情?"
        lab.textColor = UIColor.lightGrayColor()
        lab.font = UIFont.systemFontOfSize(14)
        lab.numberOfLines = 0
        return lab
        
    }()
    // MARK:--设置UI
    private func setupUI() {
        //添加控件
        addSubview(placeHolderLab)
        
        addSubview(pictureView)
        //约束
        placeHolderLab.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint.init(item: placeHolderLab, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 7))
        addConstraint(NSLayoutConstraint.init(item: placeHolderLab, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 10))
         addConstraint(NSLayoutConstraint.init(item: placeHolderLab, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: width - 20))
         addConstraint(NSLayoutConstraint.init(item: placeHolderLab, attribute: .Height, relatedBy: .Equal, toItem: placeHolderLab, attribute: .Height, multiplier: 1, constant: 0))

        pictureView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint.init(item: pictureView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: pictureView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint.init(item: pictureView, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: -20))
        addConstraint(NSLayoutConstraint.init(item: pictureView, attribute: .Height, relatedBy: .Equal, toItem: pictureView, attribute: .Width, multiplier: 1, constant: 0))

    }
}
